package com.lzy.wzy.controller;

import com.alibaba.fastjson.JSON;
import com.lzy.wzy.constant.MyConstant;
import com.lzy.wzy.model.ExceptionReportBean;
import com.lzy.wzy.model.UpTemplateDataBean;
import com.lzy.wzy.vo.LastRecordVo;
import com.lzy.wzy.result.Result;
import com.lzy.wzy.result.ResponseCode;
import com.lzy.wzy.service.TemplateService;
import com.lzy.wzy.utils.PatternUtils;
import com.lzy.wzy.vo.ProductionVo;
import com.lzy.wzy.vo.TemplateVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("/produce")
@Slf4j
public class TemplateController {

    @Autowired
    private TemplateService templateService;

    @PreAuthorize("hasAnyAuthority('ROLE_PLAN','ROLE_PRODUCE','ROLE_CRAFT','ROLE_QUALITY','QUALITY_INSPECTION','ROLE_ADMIN','ROLE_OTHER')")
    @RequestMapping(value = "/getTemplate", method = RequestMethod.GET)
    public Result<TemplateVo> getTemplate(@NotBlank @RequestParam("templateId") String templateId, @NotBlank @RequestParam("workOrderNumber") String workOrderNumber) {
        if (PatternUtils.checkDigitId(templateId) || !PatternUtils.checkWorkOrderNumber(workOrderNumber)) {
            return new Result<>(ResponseCode.FAILURE.getCode(), ResponseCode.FAILURE.getMsg(), null);
        }
        return templateService.doQueryTemplate(Integer.parseInt(templateId.trim()), workOrderNumber);
    }


//    @PreAuthorize("hasAnyAuthority('ROLE_PRODUCE')")
//    @RequestMapping(value = "/saveData", method = RequestMethod.POST)
//    public Result<T> savaData(@Validated @RequestBody UpTemplateDataBean upTemplateDataBean) {
//        log.info("上传生产数据");
//        return templateService.doUpTemplateData(upTemplateDataBean);
//    }

    @PreAuthorize("hasAnyAuthority('ROLE_PRODUCE','ROLE_ADMIN')")
    @RequestMapping(value = "/refreshBodyData", method = RequestMethod.POST)
    public Result<List<LinkedHashMap<String, Object>>> refreshBodyData(@NotBlank @RequestParam("templateId") String templateId, @NotEmpty @RequestParam("fieldNames[]") List<String> fieldNames, @NotBlank @RequestParam("workOrderNumber") String workOrderNumber) {
        if (PatternUtils.checkDigitId(templateId) || !PatternUtils.checkWorkOrderNumber(workOrderNumber)) {
            return new Result<>(ResponseCode.WRONG_FORMAT.getCode(), ResponseCode.WRONG_FORMAT.getMsg(), null);
        }
        return templateService.doRefreshBodyData(Integer.parseInt(templateId), fieldNames, workOrderNumber);
    }


    @PreAuthorize("hasAnyAuthority('ROLE_PRODUCE','ROLE_ADMIN')")
    @RequestMapping(value = "/stest", method = RequestMethod.POST)
    public Result<Integer> test(@RequestBody UpTemplateDataBean upTemplateDataBean) {
        System.out.println(upTemplateDataBean);

        return new Result<>(200, ResponseCode.SUCCESS.getMsg(), null);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_PRODUCE','ROLE_QUALITY','QUALITY_INSPECTION','ROLE_CRAFT','ROLE_ADMIN')")
    @RequestMapping(value = "/reportAbnormalInformation", method = RequestMethod.POST)
    public Result<T> reportAbnormalInformation(@RequestParam("exceptionReportBean") String exceptionReportBean ,  MultipartFile file) throws IOException {
        ExceptionReportBean reportBean = JSON.parseObject(exceptionReportBean, ExceptionReportBean.class);
        return templateService.doReportAbnormalInformation(reportBean,file);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_PRODUCE','ROLE_CRAFT','ROLE_QUALITY','QUALITY_INSPECTION','ROLE_ADMIN')")
    @RequestMapping(value = "/viewTheLastOperationRecord/{workOrderNumber}/{templateId}", method = RequestMethod.GET)
    public Result<LastRecordVo> viewTheLastOperationRecord(@NotBlank @PathVariable("workOrderNumber") String workOrderNumber, @NotBlank @PathVariable("templateId") String templateId) {
        if (PatternUtils.checkDigitId(templateId) || !PatternUtils.checkWorkOrderNumber(workOrderNumber)) {
            return new Result<>(ResponseCode.WRONG_FORMAT.getCode(), ResponseCode.WRONG_FORMAT.getMsg(), null);
        }
        return templateService.doViewTheLastOperationRecord(workOrderNumber, Integer.parseInt(templateId));
    }

    //发起完工
    @PreAuthorize("hasAnyAuthority('ROLE_PRODUCE','ROLE_ADMIN')")
    @RequestMapping(value = "/initiateCompletion", method = RequestMethod.POST)
    public Result<T> initiateCompletion(@NotBlank @RequestParam("workOrderNumber") String workOrderNumber, @NotBlank @RequestParam("templateId") String templateId) {
        synchronized (MyConstant.Re.r6){
            if (!PatternUtils.checkWorkOrderNumber(workOrderNumber) || PatternUtils.checkDigitId(templateId)) {
                return new Result<>(ResponseCode.WRONG_FORMAT.getCode(), ResponseCode.WRONG_FORMAT.getMsg(), null);
            }
            return templateService.doInitiateCompletion(workOrderNumber, templateId);
        }
    }


    @PreAuthorize("hasAnyAuthority('ROLE_PRODUCE')")
    @RequestMapping(value = "/getSingleData", method = RequestMethod.GET)
    public Result<String> getSingleData(@NotBlank @RequestParam("templateId") String templateId ,@NotBlank @RequestParam("field") String field , @NotBlank @RequestParam("id") String id) throws Exception {
        return templateService.doGetSingleData(Integer.parseInt(templateId),field,id) ;
    }
}
